package com.example.android.ui.home;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.media.Image;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.example.android.R;
import com.example.android.bean.Goods;
import com.example.android.ui.MainActivity;
import com.example.android.ui.addHB.AddHBActivity;
import com.example.android.ui.base.BaseFragment;
import com.example.android.ui.detail.GoodsDetailActivity;
import com.example.android.ui.home.adapter.HomeRecyclerViewAdapter;
import com.example.android.ui.home.adapter.HomeSpanSizeLookup;
import com.example.android.ui.search.SearchActivity;

import java.util.ArrayList;
import java.util.List;

public class HomeFragment extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener, HomeContract.IHomeView, HomeRecyclerViewAdapter.OnItemClickListener, View.OnClickListener {
    private HomeRecyclerViewAdapter homeRecyclerViewAdapter;
    private HomePresenter homePresenter;
    private HomeSpanSizeLookup homeSpanSizeLookup;
    private SwipeRefreshLayout swipeRefreshLayout;
    private PopupWindow mPopWindow;
    private EditText home_search;
    private ImageView add, home_scan;
    private static final String TAG = "HomeFragment";

    @Override
    protected void initViews() {
        swipeRefreshLayout = find(R.id.home_swiperefresh);
        swipeRefreshLayout.setOnRefreshListener(this);


        RecyclerView recyclerView = find(R.id.home_recycler);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 4);
        List<Goods> goodsList = new ArrayList<>();
        homeSpanSizeLookup = new HomeSpanSizeLookup(goodsList);
        gridLayoutManager.setSpanSizeLookup(homeSpanSizeLookup);
        recyclerView.setLayoutManager(gridLayoutManager);


        homeRecyclerViewAdapter = new HomeRecyclerViewAdapter(recyclerView, getActivity(), goodsList);
        homeRecyclerViewAdapter.setOnItemClickListener(this);
        recyclerView.setAdapter(homeRecyclerViewAdapter);

        homePresenter = new HomePresenter(this);
        homePresenter.getData();


        home_search = find(R.id.home_search);
        home_scan = find(R.id.home_scan);
        add = find(R.id.home_image_add);
        home_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });

        //监听事件
        home_search.setOnClickListener(this);
        home_scan.setOnClickListener(this);
        add.setOnClickListener(this);

    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_home;
    }

    @Override
    public void onRefresh() {
        swipeRefreshLayout.setRefreshing(false);
        homePresenter.getData();
    }

    @Override
    public void getGoodsSuccess(List<Goods> goodsList) {
        homeSpanSizeLookup.setGoods(goodsList);
        homeRecyclerViewAdapter.setGoods(goodsList);
    }

    @Override
    public void getGoodsError(Throwable throwable) {

    }

    @Override
    public void onItemClick(Goods goods) {
//        Log.i(TAG,"onItemClick" + goods);
        Intent intent = new Intent(getActivity(), GoodsDetailActivity.class);
        intent.putExtra(GoodsDetailActivity.GOODS_ID, goods.getGoodsId());
        startActivity(intent);

    }

    public void showPopupWindow() {
        View contentView = LayoutInflater.from(getActivity()).inflate(R.layout.pop_window, null);
        mPopWindow = new PopupWindow(contentView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
        mPopWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        mPopWindow.setOutsideTouchable(true);
        mPopWindow.setOutsideTouchable(true);
        mPopWindow.setFocusable(true);
        mPopWindow.setAnimationStyle(R.style.pop_add);
        TextView add_goods = contentView.findViewById(R.id.pop_add_goods);
        TextView add_hb = contentView.findViewById(R.id.pop_add_hb);
        add_hb.setOnClickListener(this);
        add_goods.setOnClickListener(this);
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.activity_main, null);
        mPopWindow.showAsDropDown(add, -150, 10);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.home_scan:
                break;
            case R.id.home_search:
                Intent intent = new Intent(getActivity(), SearchActivity.class);
                startActivity(intent);
                break;
            case R.id.home_image_add:
                showPopupWindow();
                break;
            case R.id.pop_add_goods:
                break;
            case R.id.pop_add_hb:
                Intent intent1 = new Intent(getActivity(), AddHBActivity.class);
                startActivity(intent1);
                break;
            default:
                break;

        }
    }
}
